﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Seller
{
    public partial class mfCrecityfyAlipayAccount : msBsc.Cls.mBPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        private void InitCmfInfo()
        {
            if (!msBsc.SvRef.UserInfo.Online)
            {
                this.ExecJScript(@"window.location.href='/mp_UserLogin.htm'");
                return;
            }

            if (!msBsc.SvRef.UserInfo.UsrIsCmfInnerPwd)
            {
                this.ExecJScript(@"window.location.href=""ms_CmfInnerPayPwd.htm?ReturnUrl=/ms_CrecityfyAlipayAccount.htm""");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                System.Data.DataSet ds = objCBizCustomerBaseInfo.SelectById(msBsc.SvRef.UserInfo.Operator);

                if (ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"] == DBNull.Value
                    || string.IsNullOrEmpty(ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString()))
                {
                    this.ExecJScript(@"window.location.href=""ms_BindBalanceInfo.htm""");
                    return;
                }

                if (ds.Tables[0].Rows[0]["CUSTOMER_CREFY_STATUS"] == DBNull.Value
                    || Convert.ToInt32(ds.Tables[0].Rows[0]["CUSTOMER_CREFY_STATUS"]) != (int)msBsc.AppLogic.DataAlc.CrefEnum.PayMoney)
                {
                    this.ExecJScript(@"alert(""平台未向您的账户转入验证金额，请收到后再进行验证！"");window.location.href=""ms_DjackInfo.htm""");
                    return;
                }

                //if (ds.Tables[0].Rows[0]["CUSTOMER_IS_CREFY"] != DBNull.Value
                //    && Convert.ToBoolean(ds.Tables[0].Rows[0]["CUSTOMER_IS_CREFY"]) == true
                //    && Convert.ToInt32(ds.Tables[0].Rows[0]["CUSTOMER_CREFY_STATUS"]) == (int)msBsc.AppLogic.DataAlc.CrefEnum.PassCref)
                //{
                //    this.ExecJScript(@"alert(""您的账户已经认证过，不需要再次认证！"");window.location.href=""ms_DjackInfo.htm""");
                //    return;
                //}

                if (ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"] != DBNull.Value
                    && Convert.ToInt32(ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"]) > 3)
                {
                    TimeSpan tis = objCBizCustomerBaseInfo.dtCurrDateTime - Convert.ToDateTime(ds.Tables[0].Rows[0]["CUSTOMER_CREFY_LAST_DT"]);

                    if (tis.Days < 30)
                    {
                        this.ExecJScript("alert('您的账户存在异常操作，在30天内不能进行认证！'" + (30 - tis.Days) + "'天后,可以进行再次认证');window.location.href='ms_DjackInfo.htm'");
                        return;
                    }
                    else
                    {
                        ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"] = 0;
                        objCBizCustomerBaseInfo.Update(ds);
                    }
                }


                int currentCreNum = 3 - Convert.ToInt32(ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"] == DBNull.Value ? "0" : ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"]);

                this.ExecJScript("ShowBaseInfo('" + currentCreNum + "','" + ds.Tables[0].Rows[0]["CUSTOMER_ALIPAY_ACCOUNT"].ToString() + "')");
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "InitInfo")
            {
                InitCmfInfo();
            }
            if (this.strAction == "checkAccount")
            {
                CheckUserAccount();
            }
        }

        private void CheckUserAccount()
        {
            string moneyC = this.GetParamHtValue("money");
            if (string.IsNullOrEmpty(moneyC))
            {
                this.Alert("请输入收到的金额");
                return;
            }

            decimal dsMoney;

            if (!decimal.TryParse(moneyC, out dsMoney))
            {
                this.Alert("请输入正确的数字");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerBaseInfo objCBizCustomerBaseInfo = new AppLogic.DataAlc.CBizCustomerBaseInfo())
            {
                if (!msBsc.SvRef.UserInfo.Online)
                {
                    this.ExecJScript(@"window.location.href='/mp_UserLogin.htm'");
                    return;
                }

                System.Data.DataSet ds = objCBizCustomerBaseInfo.SelectById(msBsc.SvRef.UserInfo.Operator);

                if (dsMoney != Convert.ToDecimal(ds.Tables[0].Rows[0]["SYS_FOR_CREFY"] == DBNull.Value ? "0" : ds.Tables[0].Rows[0]["SYS_FOR_CREFY"]))
                {
                    ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"] = Convert.ToInt32(ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"] == DBNull.Value ? "0" : ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"]) + 1;
                    ds.Tables[0].Rows[0]["CUSTOMER_CREFY_LAST_DT"] = objCBizCustomerBaseInfo.dtCurrDateTime;
                    if (Convert.ToInt32(ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"] == DBNull.Value ? "0" : ds.Tables[0].Rows[0]["CUSTOMER_CREFY_FAILD_NUM"]) > 3)
                    {
                        ds.Tables[0].Rows[0]["CUSTOMER_CREFY_STATUS"] = (int)msBsc.AppLogic.DataAlc.CrefEnum.FialedCref;
                    }

                    objCBizCustomerBaseInfo.Update(ds);

                    this.ExecJScript("alert('输入的金额与认证金额不等，认证失败！请重新输入！');window.location.href='/ms_CrecityfyAlipayAccount.htm'");
                    return;
                }

                DateTime currentDt = objCBizCustomerBaseInfo.dtCurrDateTime;
                ds.Tables[0].Rows[0]["CUSTOMER_CREFY_LAST_DT"] = currentDt;
                ds.Tables[0].Rows[0]["CUSTOMER_CREFY_STATUS"] = (int)msBsc.AppLogic.DataAlc.CrefEnum.PassCref;
                ds.Tables[0].Rows[0]["CUSTOMER_CREFY_DT"] = currentDt;
                ds.Tables[0].Rows[0]["CUSTOMER_IS_CREFY"] = 1;
                objCBizCustomerBaseInfo.Update(ds);

                this.ExecJScript("alert('账户认证成功!');window.location.href='/ms_DjackInfo.htm'");

            }
        }
    }
}